<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">.unbind()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/events/">事件</a> &gt; <a href="/category/events/event-handler-attachment/">绑定事件处理器</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/deprecated/">弃用</a> &gt; <a href="/category/deprecated/deprecated-3.0/">3.0 版本弃用的 API</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/unbind/" target="_blank">.unbind()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="unbind1"><h2 class="section-title">
<span class="name">.unbind(  [eventType ] [, handler(eventObject) ] )</span><span class="returns">返回: <a href="/Types/#jQuery">jQuery</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>从元素上删除一个以前附加事件处理程序。</p>
<ul class="signatures">
<li class="signature" id="unbind-eventType-handlereventObject">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.unbind( [eventType ] [,
			            handler(eventObject) ] )</h4>
<ul>
<li>
<div><strong>eventType</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>以后包含JavaScript事件类型的字符串，比如<code>click</code> 或 <code>submit</code>。</div>
</li>
<li>
<div><strong>handler(eventObject)</strong></div>
<div>类型: <a href="/Types/#Function">Function</a>()</div>
<div>这个函数今后不在执行。</div>
</li>
</ul>
</li>
<li class="signature" id="unbind-eventType-false">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4.3/">1.4.3</a></span>.unbind( eventType,
			            false )</h4>
<ul>
<li>
<div><strong>eventType</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>以后包含JavaScript事件类型的字符串，比如<code>click</code> 或 <code>submit</code>。</div>
</li>
<li>
<div><strong>false</strong></div>
<div>类型: <a href="/Types/#Boolean">Boolean</a>
</div>
<div>解除绑定相应的'return false'函数，这个函数使用<code>.bind( eventType, false )</code>绑定。</div>
</li>
</ul>
</li>
<li class="signature" id="unbind-event">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.unbind( event )</h4>
<ul>
<li>
<div><strong>event</strong></div>
<div>类型: <a href="/Types/#Object">Object</a>
</div>
<div>一个JavaScript事件对象传递给一个事件处理器。</div>
</li>
</ul>
</li>
<li class="signature">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>.unbind()
			        </h4>
<ul>
<li>
<div class="null-signature">此签名不接受任何参数。</div>
</li>
</ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc">
<p>每个用<code>.bind()</code>方法绑定的事件处理程序可以使用<code>.unbind()</code>移除。（从 jQuery 1.7开始， <a href="/on"><code>.on()</code></a> 和 <a href="/off"><code>.off()</code></a>方法是最好的元素上附加和移除事件处理程序的方法。）在最简单的情况下，不带参数的<code>.unbind()</code>
			    将移除元素上所有绑定的处理程序。</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container">
			                    <div class="line"><code>$(<span class="string">'#foo'</span>).unbind();</code></div>
			                </div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>上面的代码删除所有事件类型的处理程序。为了更加精确，我们可以传递一个事件类型：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container">
			                    <div class="line"><code>$(<span class="string">'#foo'</span>).unbind(<span class="string">'click'</span>);</code></div>
			                </div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>通过指定<code>click</code>事件类型，
			    只有该事件类型的处理程序将解除绑定。但是，
			    这种方法有负面影响，这是因为有可能在代码的其它地方在相同的元素上绑定了相同的事件处理。为了避免该问题，对于一个健壮性和扩展性强的应用而言，通过在使用该方法时，会传入两个参数：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> handler = <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  alert( <span class="string">"The quick brown fox jumps over the lazy dog."</span> );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).bind( <span class="string">"click"</span>, handler );</code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).unbind( <span class="string">"click"</span>, handler );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>通过传入 handler 参数，我们可以确保不会意外的移除其它的函数。但是需要注意的是，如下用法是无法正常工作的：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).bind( <span class="string">"click"</span>, <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  alert( <span class="string">"The quick brown fox jumps over the lazy dog."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Will NOT work</span></code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).unbind( <span class="string">"click"</span>, <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  alert( <span class="string">"The quick brown fox jumps over the lazy dog."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>尽管上述代码中，两个匿名函数的内容是一样的，但是它们是在不同的地方被创建的，因此 JavaScript 会将它们当成是不同的函数对象。若要解除绑定特定的事件处理函数，我们需要的是指向该函数的引用，而不是内容相同的不同函数。</p>
<div class="warning">
<p>
<strong>注意:</strong> 使用代理函数解绑元素上的事件并不会解绑该元素上所有的代理函数，因为相同的代理函数是作用于所有被代理的事件。可以使用事件上唯一的类名来解除绑定特定的事件(例如，<code>click.proxy1</code>,
			        <code>click.proxy2</code>)。
			    </p>
</div>
<h4>Using Namespaces（使用命名空间）</h4>
<p>为了解除绑定处理程序，我们可以命名空间这个事件和使用此功能来缩小范围，我们轻松解除绑定的行动。上面讨论的<code>.bind()</code>方法，
			    当绑定一个处理程序时，命名空间的定义通过使用一个点（.）字符：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).bind( <span class="string">"click.myEvents"</span>, handler );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>当一个处理程序以这种方式绑定，我们仍然可以正常的方式解除：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).unbind( <span class="string">"click"</span> );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>但是，如果我们要避免影响其他处理程序，我们可以更具体：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).unbind( <span class="string">"click.myEvents"</span> );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>我们也可以解除命名空间中所有的处理程序，无论事件类型：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).unbind( <span class="string">".myEvents"</span> );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>
			    当我们开发插当进行插件开发或今后的代码可能与其它事件处理相交互时，使用命名空间来进行事件绑定就是很好的做法。</p>
<h4>Using the Event Object（使用 Event 对象）</h4>
<p>
			    当我们希望解除从自身内部处理程序时可以使用<code>.unbind()</code>方法的第三种形式。
			     例如，假设我们希望触发一个事件处理程序只有三次：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> timesClicked = <span class="number">0</span>;</code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#foo"</span> ).bind( <span class="string">"click"</span>, <span class="keyword">function</span>( event ) {</code></div></div><div class="container"><div class="line"><code>  alert( <span class="string">"The quick brown fox jumps over the lazy dog."</span> );</code></div></div><div class="container"><div class="line"><code>  timesClicked++;</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">if</span> ( timesClicked &gt;= <span class="number">3</span> ) {</code></div></div><div class="container"><div class="line"><code>    $( <span class="keyword">this</span> ).unbind( event );</code></div></div><div class="container"><div class="line"><code>  }</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>这种情况下，事件处理函数必须带有一个参数，这样我们就可以捕获 event 对象，并且使用它在第三点击时，移除该事件处理。event 对象包含 <code>.unbind()</code>
			    所需的必要上下文信息，来确定需要移除的事件处理函数。
			    该例子同时也演示了闭包的用法。由于在事件处理中，引用了在函数外定义的 <code>timesClicked</code> 外部变量，因此在事件处理被调用时，该变量会持续递增。
			</p>
</div>
<section class="entry-examples" id="entry-examples">
<header><h2 class="underline">例子:</h2></header>
<div class="entry-example" id="example-0">
<h4>Example: <span class="desc">为带颜色的按钮绑定和解除事件绑定。</span>
</h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
<div class="line n37">37</div>
<div class="line n38">38</div>
<div class="line n39">39</div>
<div class="line n40">40</div>
<div class="line n41">41</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!doctype html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span> <span class="attribute">lang</span>=<span class="value">"en"</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">meta</span> <span class="attribute">charset</span>=<span class="value">"utf-8"</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">title</span>&gt;</span>unbind demo<span class="tag">&lt;/<span class="title">title</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">button</span> <span class="rules">{</span></code></div></div><div class="container"><div class="line"><code>    <span class="rule"><span class="attribute">margin</span>:<span class="value"> <span class="number">5</span>px;</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="rule">}</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">button</span><span class="id">#theone</span> <span class="rules">{</span></code></div></div><div class="container"><div class="line"><code>    <span class="rule"><span class="attribute">color</span>:<span class="value"> red;</span></span></code></div></div><div class="container"><div class="line"><code>    <span class="rule"><span class="attribute">background</span>:<span class="value"> yellow;</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="rule">}</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-1.9.1.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"theone"</span>&gt;</span>Does nothing...<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"bind"</span>&gt;</span>Bind Click<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span> <span class="attribute">id</span>=<span class="value">"unbind"</span>&gt;</span>Unbind Click<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">div</span> <span class="attribute">style</span>=<span class="value">"display:none;"</span>&gt;</span>Click!<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code><span class="function"><span class="keyword">function</span> <span class="title">aClick</span><span class="params">()</span> {</span></code></div></div><div class="container"><div class="line"><code>  $( <span class="string">"div"</span> ).show().fadeOut( <span class="string">"slow"</span> );</code></div></div><div class="container"><div class="line"><code>}</code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#bind"</span> ).click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">"#theone"</span> )</code></div></div><div class="container"><div class="line"><code>    .bind( <span class="string">"click"</span>, aClick )</code></div></div><div class="container"><div class="line"><code>    .text( <span class="string">"Can Click!"</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code>$( <span class="string">"#unbind"</span> ).click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">"#theone"</span> )</code></div></div><div class="container"><div class="line"><code>    .unbind( <span class="string">"click"</span>, aClick )</code></div></div><div class="container"><div class="line"><code>    .text( <span class="string">"Does nothing..."</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">可以像下面这样，解除绑定在段落上的所有事件：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"p"</span> ).unbind();</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-2">
<h4>Example: <span class="desc">可以像下面这样，解除绑定在段落上的 click 事件：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$( <span class="string">"p"</span> ).unbind( <span class="string">"click"</span> );</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-3">
<h4>Example: <span class="desc">通过传入的第二个参数，仅解除绑定先前绑定的事件：</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> foo = <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code>  <span class="comment">// Code to handle some kind of event</span></code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$( <span class="string">"p"</span> ).bind( <span class="string">"click"</span>, foo ); <span class="comment">// ... Now foo will be called when paragraphs are clicked ...</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>$( <span class="string">"p"</span> ).unbind( <span class="string">"click"</span>, foo ); <span class="comment">// ... foo will no longer be called.</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
</article>
</div>
</article>